package com.gitee.security;

import com.gitee.security.handler.*;
import com.gitee.security.validate.CaptchaFilter;
import com.gitee.service.impl.AdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AdminServiceImpl userDetailsService;

    @Autowired
    private LoginSuccessHandler loginSuccessHandler;

    @Autowired
    private LoginFailHandler loginFailHandler;

    @Autowired
    private LogoutHandler logoutHandler;

    @Autowired
    private PermissionDeniedHandler permissionDeniedHandler;

    @Autowired
    private CaptchaFilter captchaFilter;

    @Autowired
    private AnonymousHandler anonymousHandler;


    /**
     * 密码加密规则
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 自定义登录验证逻辑
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    /**
     * 忽略Web静态资源
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
    }


    /**
     * 请求拦截
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.addFilterBefore(captchaFilter, UsernamePasswordAuthenticationFilter.class)
            .formLogin()
            .loginProcessingUrl("/login")
                .successHandler(loginSuccessHandler)
                .failureHandler(loginFailHandler)
            .and()
            .logout()
                .logoutUrl("/logout")
                .logoutSuccessHandler(logoutHandler)
                .deleteCookies("JSESSIONID")
            .and()
            .authorizeRequests()
                .antMatchers("/login", "/logout", "/captcha").permitAll()
                .antMatchers("/auth/**").authenticated()
                .anyRequest().access("@permissionServiceImpl.hasPermission(request, authentication)")
            .and()
            .exceptionHandling()
                .accessDeniedHandler(permissionDeniedHandler)
                .authenticationEntryPoint(anonymousHandler)
            .and()
            .csrf().disable();
    }

}
